// pages/index.js
const MONTHS = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'June.', 'July.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'];
import { request } from '../../utils/ajax'
var marmot = requirePlugin("marmot") // 天气插件

Page({

    /**
     * 页面的初始数据
     */
    data: {
        year: new Date().getFullYear(),      // 年份
        month: new Date().getMonth() + 1,    // 月份
        day: new Date().getDate(),
        str: MONTHS[new Date().getMonth()],  // 月份字符串
        date: `${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()}`,

        demo1_days_style: [],
        demo2_days_style: [],
        demo4_days_style: [],
        demo5_days_style: [],
        demo6_days_style: [],

        oldDay: [], // 历史上的今天
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCalendar()

        // 历史上的今天
        // this.getHistory();

        // 老黄历
        this.selectComponent('#LHL').getLHL();

        // 生成天气海报
        this.loadWeather()
       
    },

    // 生成天气海报
    loadWeather() {
        marmot.data.setData(marmot.consts.DATA_HOOKS.USER_INFO, {
            word: '李狗蛋送你天天好心情', // 海报上的昵称
            qrcode: 'https://view.inews.qq.com/generateImage?from=0&type=7&width=300&id=153440278317805154' // 海报上的二维码
        })
        // this.setData({
        //     'posterInfo': {
        //         word: '美好的事情即将发生 比如“小蓝杯”送你双人咖啡套餐', // 海报上的昵称
        //         qrcode: 'https://view.inews.qq.com/generateImage?from=0&type=7&width=300&id=153440278317805154' // 海报上的二维码
        //     }
        // })
    },

    // 初始化日历
    getCalendar () {
        const days_count = new Date(this.data.year, this.data.month, 0).getDate();
        let demo1_days_style = new Array;
        for (let i = 1; i <= days_count; i++) {
            if (parseInt(Math.random() * 100) > 50) {
                demo1_days_style.push({
                    month: 'current', day: i, color: 'white', background: '#8497ee'
                });
            } else {
                demo1_days_style.push({
                    month: 'current', day: i, color: 'white'
                });
            }
        }
        this.setData({
            demo1_days_style
        });

        let demo2_days_style = new Array;
        for (let i = 1; i <= days_count; i++) {
            if (i == 12) {
                demo2_days_style.push({
                    month: 'current', day: i, color: '#314580', background: '#ffed09'
                });
            } else if (i == 16) {
                demo2_days_style.push({
                    month: 'current', day: i, color: 'white', background: '#30558c'
                });
            } else if (i == 21) {
                demo2_days_style.push({
                    month: 'current', day: i, color: 'white', background: '#3c5281'
                });
            } else {
                demo2_days_style.push({
                    month: 'current', day: i, color: 'white'
                });
            }
        }
        this.setData({
            demo2_days_style
        });

        let demo4_days_style = new Array;
        for (let i = 1; i <= days_count; i++) {
            if (i == 3) {
                demo4_days_style.push({
                    month: 'current', day: i, color: 'white', background: '#46c4f3'
                });
            } else if (i == 7) {
                demo4_days_style.push({
                    month: 'current', day: i, color: 'white', background: '#ffb72b'
                });
            } else if (i == 12 || i == 23 || i == 24) {
                demo4_days_style.push({
                    month: 'current', day: i, color: 'white', background: '#865fc1'
                });
            } else if (i == 21 || i == 22) {
                demo4_days_style.push({
                    month: 'current', day: i, color: 'white', background: '#eb4986'
                });
            } else {
                demo4_days_style.push({
                    month: 'current', day: i, color: 'white'
                });
            }
        }
        this.setData({
            demo4_days_style
        });

        let demo5_days_style = new Array;
        for (let i = 1; i <= days_count; i++) {
            const date = new Date(this.data.year, this.data.month - 1, i);
            if (date.getDay() == 0) {
                demo5_days_style.push({
                    month: 'current', day: i, color: '#f488cd'
                });
            } else {
                demo5_days_style.push({
                    month: 'current', day: i, color: '#a18ada'
                });
            }
        }
        demo5_days_style.push({ month: 'current', day: new Date().getDate(), color: 'white', background: '#84e7d0' });

        this.setData({
            demo5_days_style
        });

        let demo6_days_style = new Array;
        for (let i = 1; i <= days_count; i++) {
            const date = new Date(this.data.year, this.data.month - 1, i);
            if (i == 12) {
                demo6_days_style.push({
                    month: 'current', day: i, color: 'white', background: '#b49eeb'
                });
            } else if (i == 17) {
                demo6_days_style.push({
                    month: 'current', day: i, color: 'white', background: '#f5a8f0'
                });
            } else if (i == 21) {
                demo6_days_style.push({
                    month: 'current', day: i, color: 'white', background: '#aad4f5'
                });
            } else if (i == 25) {
                demo6_days_style.push({
                    month: 'current', day: i, color: 'white', background: '#84e7d0'
                });
            } else {
                demo6_days_style.push({
                    month: 'current', day: i, color: 'black'
                });
            }
        }

        this.setData({
            demo6_days_style
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    // 点击日历某一天 
    dayClick: function (event) {
        let newDate = `${event.detail.year}-${event.detail.month}-${event.detail.day}`;
        this.setData({
            year: event.detail.year,
            month: event.detail.month,
            day: event.detail.day,
            date: newDate
        })
        // 改变黄历内容
        this.selectComponent("#LHL").getLHL();
    },

    // 历史上的今天
    getHistory() {
        let that = this;
        wx.request({
            url: 'https://v.juhe.cn/todayOnhistory/queryEvent.php',
            data: {
                date: this.data.month + '/' + this.data.day,
                key: '8cb1a720e0505e4c766ab65e61eaa839'
            },
            header: {
                'content-type': 'application/json' // 默认值
            },
            method: 'GET', //（需大写）有效值：OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            dataType: 'json',
            responseType: 'text',// 设置响应的数据类型。合法值：text、arraybuffer
            success: function (res) {
                that.setData({ oldDay: res.data.result })
            },
            complete: function (res) {
                console.log(res.data);
            }// 接口调用结束的回调函数（调用成功、失败都会执行）
        })
    },
    // 查看历史详情
    toDetail(e) {
        wx.navigateTo({
            url: `./history-detail/index?id=${e.currentTarget.dataset.id}`
        })
    }
})